@extends('admin.layout')
@section('content')
    <div class="content-wrapper" style="min-height: 916px;">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>订单管理
                <small>列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 后台</a></li>
                <li><a href="#">订单管理</a></li>
                <li class="active">列表</li>
            </ol>
        </section>
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">订单</h3>
                        </div>
                        <!-- /.box-header -->
                        <div class="box-body">
                            <div id="example2_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
                                <div class="row">
                                    <form action="{{url('admin/order/index')}}" method="post">
                                        {{csrf_field()}}
                                        <div class="col-sm-6">
                                            <div class="dataTables_length" id="example1_length">
                                                <label>显示 <select name="num" aria-controls="example1"
                                                                  class="form-control input">
                                                        @if(!empty($request['num']))
                                                            <option value="{{$request['num']}}"
                                                                    style="display: none;">{{$request['num']}}</option>
                                                        @endif
                                                        <option value="10">10</option>
                                                        <option value="25">25</option>
                                                        <option value="50">50</option>
                                                        <option value="100">100</option>
                                                    </select> 条</label>
                                            </div>
                                        </div>
                                        <div class="input-group input-group col-md-6">
                                            @if(!empty($request['keyword']))
                                                <input type="text" name="keyword" class="form-control"
                                                       value="{{$request['keyword']}}" placeholder="搜索订单号..."/>
                                            @else
                                                <input type="text" name="keyword" class="form-control" value=""
                                                       placeholder="搜索订单号..."/>
                                            @endif
                                            <span class="input-group-btn"> <button type="submit"
                                                                                   class="btn btn-info btn-flat">Go!</button> </span>
                                        </div>

                                    </form>
                                </div>
                                <div class="row">
                                    <div class="col-sm-12">
                                        <table class="table table-bordered table-hover dataTable" id="example2"
                                               role="grid" aria-describedby="example2_info">
                                            <thead>
                                            <tr role="row">
                                                <th class="sorting_asc" tabindex="0" aria-controls="example2"
                                                    rowspan="1" colspan="1" aria-sort="ascending"
                                                    aria-label="Rendering engine: activate to sort column descending">ID
                                                </th>
                                                <th class="sorting" tabindex="0" aria-controls="example2" rowspan="1"
                                                    colspan="1" aria-label="Browser: activate to sort column ascending">
                                                    订单编号
                                                </th>
                                                <th class="sorting" tabindex="0" aria-controls="example2" rowspan="1"
                                                    colspan="1"
                                                    aria-label="Platform(s): activate to sort column ascending">订单状态
                                                </th>
                                                <th class="sorting" tabindex="0" aria-controls="example2" rowspan="1"
                                                    colspan="1"
                                                    aria-label="Platform(s): activate to sort column ascending">支付方式
                                                </th>
                                                <th class="sorting" tabindex="0" aria-controls="example2" rowspan="1"
                                                    colspan="1"
                                                    aria-label="Platform(s): activate to sort column ascending">配送方式
                                                </th>
                                                <th class="sorting" tabindex="0" aria-controls="example2" rowspan="1"
                                                    colspan="1"
                                                    aria-label="Platform(s): activate to sort column ascending">备注
                                                </th>
                                                <th class="sorting" tabindex="0" aria-controls="example2" rowspan="1"
                                                    colspan="1"
                                                    aria-label="Platform(s): activate to sort column ascending">订单提交时间
                                                </th>
                                                <th class="sorting" tabindex="0" aria-controls="example2" rowspan="1"
                                                    colspan="1"
                                                    aria-label="Engine version: activate to sort column ascending">操作
                                                </th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            @foreach($data as $order)
                                                <tr role="row" class="odd">
                                                    <td class="sorting_1">{{$order -> id}}</td>
                                                    <td>{{$order -> ordercode}}</td>
                                                    {{--TODO: 订单显示权限判断--}}
                                                    {{--如果是管理员,直接显示<td></td>内容--}}
                                                    @if(true)
                                                        <td>
                                                            @if($order -> status == 0)
                                                                待商家确认订单
                                                            @elseif($order -> status == 1)
                                                                商家制作中
                                                            @elseif($order -> status == 2)
                                                                商家配送中
                                                            @elseif($order -> status == 3)
                                                                用户待评价
                                                            @elseif($order -> status == 4)
                                                                订单完成
                                                            @elseif($order -> status == 5)
                                                                无效订单
                                                            @endif
                                                        </td>
                                                    @else

                                                        <td style="padding: 0px" class="orderStatus" name="orderStatus"
                                                            orderstatus="{{$order -> status}}">
                                                            @if($order -> status == 0)
                                                                <select type='text'
                                                                        class='orderStatus form-control input'
                                                                        style="width: 150px;">
                                                                    <option value="{{$order -> status}}"
                                                                            style="display: none">订单等待确认
                                                                    </option>
                                                                    <option value="{{$order -> status}}">订单确认,制作中
                                                                    </option>
                                                                </select>
                                                            @elseif($order -> status == 1)
                                                                <select type='text'
                                                                        class='orderStatus form-control input'
                                                                        style="width: 150px;">
                                                                    <option value="{{$order -> status}}"
                                                                            style="display: none">订单确认,制作中
                                                                    </option>
                                                                    <option value="{{$order -> status}}">订单配送中</option>
                                                                </select>
                                                            @elseif($order -> status == 2)
                                                                <div style="padding: 8px">
                                                                    订单配送中
                                                                </div>
                                                            @elseif($order -> status == 3)
                                                                <div style="padding: 8px">
                                                                    用户已收货,待评价
                                                                </div>

                                                            @elseif($order -> status == 4)
                                                                <div style="padding: 8px">
                                                                    完成的订单
                                                                </div>

                                                            @else
                                                                <div style="padding: 8px">
                                                                    无效的订单
                                                                </div>

                                                            @endif
                                                        </td>
                                                    @endif


                                                    {{--</span>--}}
                                                    {{--</td>--}}
                                                    <td>
                                                        @if($order -> payway == 0)
                                                            在线支付
                                                        @elseif($order -> payway == 1)
                                                            货到付款
                                                        @else
                                                            无效支付
                                                        @endif
                                                    </td>
                                                    <td>{{$order -> sendway}}</td>
                                                    <td>{{$order -> order_bak}}</td>
                                                    <td>{{$order -> created_at}}</td>
                                                    <td>
                                                        {{--TODO: "编辑"权限--}}
                                                        {{--@if(true)--}}
                                                            {{--<a href="{{asset('admin/address/edit')}}">编辑　</a>--}}
                                                        {{--@endif--}}
                                                        <a href="{{asset('admin/order/delete')}}/{{$order->id}}">删除</a>
                                                    </td>
                                                </tr>
                                            @endforeach
                                            </tbody>
                                        </table>
                                        {!! $data->appends($request)->render() !!}
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                    </div>
                    <!-- /.box -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
    <script type="text/javascript">
        window.onload = function () {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

//            console.log($("option[value=1]"));
            $("option[value='1']").each(function () {
                if ($(this).html() == "订单配送中" && $(this).val() == '1') {
//                    window.location.href = window.location.href;
                }
            });


            $("td[name='orderStatus']").each(function () {
                $(this).on('change ', function () {
                    switch ($(this).attr('orderstatus')) {
                        case '0':
//                            $(this).attr('orderstatus', '1');
                            var orderId = $(this).parent().children().first().html();

//                                alert(orderId);
                            // 用 post 提交 ajax
                            $.post('/admin/order/ajaxUpdate', {
                                orderstatus: $(this).attr('orderstatus'),
                                id: orderId
                            }, function ($data) {
//                                    alert($data);
                                alert('此操作不可逆');
                                window.location.href = window.location.href;
                            }, 'json');
                            break;
                        case '1':
                            var orderId = $(this).parent().children().first().html();
                            $.post('/admin/order/ajaxUpdate', {
                                orderstatus: $(this).attr('orderstatus'),
                                id: orderId
                            }, function ($data) {
                                alert('此操作不可逆');

//                                $("<option>订单确认,制作中</option>" +
//                                        "<option>订单配送中</option>");
                                window.location.href = window.location.href;
                            }, 'json');
//                            $(this).attr('orderstatus', '2');
                            var orderId = $(this).parent().children().first().html();
                            $("<td>订单配送中</td>").replaceAll($(this));
                            break;
                        default:
                    }
                });
            });
        }
    </script>
@endsection